{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4c87bc55-5547-4079-a6d8-41e8198d2356",
   "metadata": {},
   "source": [
    "# 快速入门 Assistants API \n",
    "\n",
    "Assistants API 允许您在自己的应用程序中构建人工智能助手。一个助手有其指令，并可以利用模型、工具和知识来回应用户查询。\n",
    "\n",
    "Assistants API 目前支持三种类型的工具：\n",
    "- 代码解释器 Code Interpreter\n",
    "- 检索 Retrieval\n",
    "- 函数调用 Function calling\n",
    "\n",
    "使用 [Playground](https://platform.openai.com/playground?mode=assistant) 可以在线探索和测试 Assistants API 功能。\n",
    "\n",
    "本入门指南将指导您完成创建和运行使用代码解释器的助手的关键步骤，以下是使用 Assistants API 标准流程：\n",
    "1. 通过定义其自定义指令并选择 LLM 来创建一个助手(Assistant)。如果有需求，可以添加文件并启用诸如代码解释器、检索和函数调用等工具。\n",
    "2. 当用户开始对话时，创建一个线程(Thread)。\n",
    "3. 当用户提问时，向线程添加消息(Messages)。\n",
    "4. 通过调用模型和工具在线程上运行助手以生成响应。\n",
    "\n",
    "![assistans](images/diagram-assistant.png)\n",
    "\n",
    "OBJECT | WHAT IT REPRESENTS\n",
    "--- | ---\n",
    "Assistant | 专为特定目的构建的人工智能，使用 OpenAI 的模型并调用工具\n",
    "Thread | 助手与用户之间的对话会话。线程存储消息，并自动处理截断，以将内容适应模型的上下文。\n",
    "Message | 由助手或用户创建的消息。消息可以包括文本、图片和其他文件。消息以列表形式存储在线程上。\n",
    "Run | 在线程上对一个助手的调用。助手利用其配置和线程的消息执行任务，通过调用模型和工具。作为运行的一部分，助手会将消息追加到线程中。\n",
    "Run Step | 助手在运行中采取的详细步骤列表。助手可以在其运行期间调用工具或创建消息。检查运行步骤可以让您深入了解助手如何得出最终结果。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7a8bbea-2f1f-45a4-b3e8-929614c66790",
   "metadata": {},
   "source": [
    "\n",
    "## 使用 Assistants 开发数学辅导老师\n",
    "\n",
    "在这个示例中，我们正在创建一个数学辅导助手，并启用了代码解释器工具。\n",
    "\n",
    "### 第一步：创建助手"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "052b55e0-8170-41de-a721-0961606c793e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai  # 导入 openai 库\n",
    "\n",
    "# 从环境变量 OPENAI_API_KEY 中获取 API 密钥\n",
    "client = openai.OpenAI()\n",
    "\n",
    "# 创建一个名为 \"Math Tutor\" 的助手，它是一个个人数学辅导老师。这个助手能够编写并运行代码来解答数学问题。\n",
    "assistant = client.beta.assistants.create(\n",
    "    name=\"Math Tutor\",\n",
    "    instructions=\"You are a personal math tutor. Write and run code to answer math questions.\",\n",
    "    tools=[{\"type\": \"code_interpreter\"}],  # 使用工具：代码解释器\n",
    "    model=\"gpt-4-1106-preview\",  # 使用模型： GPT-4\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1106c286-aa3b-4076-9992-37b1a56743c6",
   "metadata": {},
   "source": [
    "### 第二步：创建线程\n",
    "\n",
    "一个线程代表用户和一个或多个助手之间的对话。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d1c7e57e-8e95-4411-ae53-0ae3ac004c62",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建一个交流线程\n",
    "thread = client.beta.threads.create()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b958896-d802-445e-8098-0a443399cf43",
   "metadata": {},
   "source": [
    "### 第三步：往线程添加消息\n",
    "\n",
    "用户或APP创建的消息内容将作为消息对象（Message Object）添加到线程中。 \n",
    "\n",
    "消息可以包含文本和文件，向线程添加的消息数量没有限制 - OpenAI 会智能地截断任何不适合模型上下文窗口的内容。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f372ca8f-53be-4c3c-9e0a-c1b41b8955e7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 在该线程中创建一条用户消息，并提交一个数学问题：“我需要解方程 `3x + 11 = 14`。你能帮忙吗？”\n",
    "message = client.beta.threads.messages.create(\n",
    "    thread_id=thread.id,\n",
    "    role=\"user\",\n",
    "    content=\"I need to solve the equation `3x + 11 = 14`. Can you help me?\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c525954-63ef-44b1-94de-75a5b43568da",
   "metadata": {},
   "source": [
    "### 第四步：调用助手\n",
    "\n",
    "一旦所有用户消息都添加到了线程中，你可以使用任何助手运行该线程。\n",
    "\n",
    "创建一个运行会使用与助手相关的模型和工具来生成响应。这些响应将作为助手消息添加到线程中。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7a77c01e-7a68-41da-909b-d8e675a50d26",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Run completed with status: completed\n",
      "\n",
      "Messages:\n",
      "\n",
      "Role: Assistant\n",
      "Message:\n",
      "The solution to the equation \\(3x + 11 = 14\\) is \\(x = 1\\). If you need additional help with this equation or another one, feel free to ask, Jane.\n",
      "\n",
      "Role: Assistant\n",
      "Message:\n",
      "Of course, Jane! To solve the equation \\(3x + 11 = 14\\), we need to isolate the variable \\(x\\) on one side of the equation. We can do this by following these steps:\n",
      "\n",
      "1. Subtract 11 from both sides of the equation to get the term with \\(x\\) by itself on one side.\n",
      "2. Divide both sides of the resulting equation by 3 to solve for \\(x\\).\n",
      "\n",
      "Let's do these calculations to find the value of \\(x\\).\n",
      "\n",
      "Role: User\n",
      "Message:\n",
      "I need to solve the equation `3x + 11 = 14`. Can you help me?\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 创建并等待执行流完成，用于处理该线程中的交互和问题解答\n",
    "run = client.beta.threads.runs.create_and_poll(\n",
    "    thread_id=thread.id,\n",
    "    assistant_id=assistant.id,\n",
    "    instructions=\"Please address the user as Jane Doe. The user has a premium account.\",  # 以 Jane Doe 称呼用户，并且用户拥有高级账户\n",
    ")\n",
    "\n",
    "print(\"Run completed with status: \" + run.status)  # 打印执行流的完成状态\n",
    "\n",
    "# 如果执行流状态为 \"completed\"（已完成），则获取并打印所有消息\n",
    "if run.status == \"completed\":\n",
    "    messages = client.beta.threads.messages.list(thread_id=thread.id)\n",
    "\n",
    "    print(\"\\nMessages:\\n\")\n",
    "    for message in messages:\n",
    "        assert message.content[0].type == \"text\"\n",
    "        print(f\"Role: {message.role.capitalize()}\")  # 角色名称首字母大写\n",
    "        print(\"Message:\")\n",
    "        print(message.content[0].text.value + \"\\n\")  # 每条消息后添加空行以增加可读性"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea5e1bca-3401-4db8-b3ae-b2797d11680b",
   "metadata": {},
   "source": [
    "### 通过 Assistant ID 删除指定助手"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7535ee48-2107-465d-b5fa-b12356ba8b87",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AssistantDeleted(id='asst_CmikkRdSAUDlb5dBDqHX57dT', deleted=True, object='assistant.deleted')"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 删除创建的助手\n",
    "client.beta.assistants.delete(assistant.id)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "025b777a-d279-4c62-8a50-7d24a1baaa5a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "929b787f-372b-4c35-9769-381d41c91042",
   "metadata": {},
   "source": [
    "### 使用流式输出实现数学辅导老师"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8677cd78-a76e-44c2-909a-1bca20b72b72",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "starting run stream\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"run_0PqlNbmtft5Nz6F8eaYsP1vU\",\n",
      "    \"assistant_id\": \"asst_9UdKwJ8S7iOMrlEX6lzXpLWD\",\n",
      "    \"cancelled_at\": null,\n",
      "    \"completed_at\": null,\n",
      "    \"created_at\": 1713409009,\n",
      "    \"expires_at\": 1713409609,\n",
      "    \"failed_at\": null,\n",
      "    \"file_ids\": [],\n",
      "    \"incomplete_details\": null,\n",
      "    \"instructions\": \"Please address the user as Jane Doe. The user has a premium account.\",\n",
      "    \"last_error\": null,\n",
      "    \"max_completion_tokens\": null,\n",
      "    \"max_prompt_tokens\": null,\n",
      "    \"metadata\": {},\n",
      "    \"model\": \"gpt-4-1106-preview\",\n",
      "    \"object\": \"thread.run\",\n",
      "    \"required_action\": null,\n",
      "    \"response_format\": \"auto\",\n",
      "    \"started_at\": null,\n",
      "    \"status\": \"queued\",\n",
      "    \"thread_id\": \"thread_rudF1jmbDRotBYIm3RVJIoX0\",\n",
      "    \"tool_choice\": \"auto\",\n",
      "    \"tools\": [\n",
      "      {\n",
      "        \"type\": \"code_interpreter\"\n",
      "      }\n",
      "    ],\n",
      "    \"truncation_strategy\": {\n",
      "      \"type\": \"auto\",\n",
      "      \"last_messages\": null\n",
      "    },\n",
      "    \"usage\": null,\n",
      "    \"temperature\": 1.0,\n",
      "    \"top_p\": 1.0\n",
      "  },\n",
      "  \"event\": \"thread.run.created\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"run_0PqlNbmtft5Nz6F8eaYsP1vU\",\n",
      "    \"assistant_id\": \"asst_9UdKwJ8S7iOMrlEX6lzXpLWD\",\n",
      "    \"cancelled_at\": null,\n",
      "    \"completed_at\": null,\n",
      "    \"created_at\": 1713409009,\n",
      "    \"expires_at\": 1713409609,\n",
      "    \"failed_at\": null,\n",
      "    \"file_ids\": [],\n",
      "    \"incomplete_details\": null,\n",
      "    \"instructions\": \"Please address the user as Jane Doe. The user has a premium account.\",\n",
      "    \"last_error\": null,\n",
      "    \"max_completion_tokens\": null,\n",
      "    \"max_prompt_tokens\": null,\n",
      "    \"metadata\": {},\n",
      "    \"model\": \"gpt-4-1106-preview\",\n",
      "    \"object\": \"thread.run\",\n",
      "    \"required_action\": null,\n",
      "    \"response_format\": \"auto\",\n",
      "    \"started_at\": null,\n",
      "    \"status\": \"queued\",\n",
      "    \"thread_id\": \"thread_rudF1jmbDRotBYIm3RVJIoX0\",\n",
      "    \"tool_choice\": \"auto\",\n",
      "    \"tools\": [\n",
      "      {\n",
      "        \"type\": \"code_interpreter\"\n",
      "      }\n",
      "    ],\n",
      "    \"truncation_strategy\": {\n",
      "      \"type\": \"auto\",\n",
      "      \"last_messages\": null\n",
      "    },\n",
      "    \"usage\": null,\n",
      "    \"temperature\": 1.0,\n",
      "    \"top_p\": 1.0\n",
      "  },\n",
      "  \"event\": \"thread.run.queued\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"run_0PqlNbmtft5Nz6F8eaYsP1vU\",\n",
      "    \"assistant_id\": \"asst_9UdKwJ8S7iOMrlEX6lzXpLWD\",\n",
      "    \"cancelled_at\": null,\n",
      "    \"completed_at\": null,\n",
      "    \"created_at\": 1713409009,\n",
      "    \"expires_at\": 1713409609,\n",
      "    \"failed_at\": null,\n",
      "    \"file_ids\": [],\n",
      "    \"incomplete_details\": null,\n",
      "    \"instructions\": \"Please address the user as Jane Doe. The user has a premium account.\",\n",
      "    \"last_error\": null,\n",
      "    \"max_completion_tokens\": null,\n",
      "    \"max_prompt_tokens\": null,\n",
      "    \"metadata\": {},\n",
      "    \"model\": \"gpt-4-1106-preview\",\n",
      "    \"object\": \"thread.run\",\n",
      "    \"required_action\": null,\n",
      "    \"response_format\": \"auto\",\n",
      "    \"started_at\": 1713409009,\n",
      "    \"status\": \"in_progress\",\n",
      "    \"thread_id\": \"thread_rudF1jmbDRotBYIm3RVJIoX0\",\n",
      "    \"tool_choice\": \"auto\",\n",
      "    \"tools\": [\n",
      "      {\n",
      "        \"type\": \"code_interpreter\"\n",
      "      }\n",
      "    ],\n",
      "    \"truncation_strategy\": {\n",
      "      \"type\": \"auto\",\n",
      "      \"last_messages\": null\n",
      "    },\n",
      "    \"usage\": null,\n",
      "    \"temperature\": 1.0,\n",
      "    \"top_p\": 1.0\n",
      "  },\n",
      "  \"event\": \"thread.run.in_progress\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"assistant_id\": \"asst_9UdKwJ8S7iOMrlEX6lzXpLWD\",\n",
      "    \"cancelled_at\": null,\n",
      "    \"completed_at\": null,\n",
      "    \"created_at\": 1713409010,\n",
      "    \"failed_at\": null,\n",
      "    \"last_error\": null,\n",
      "    \"object\": \"thread.run.step\",\n",
      "    \"run_id\": \"run_0PqlNbmtft5Nz6F8eaYsP1vU\",\n",
      "    \"status\": \"in_progress\",\n",
      "    \"step_details\": {\n",
      "      \"tool_calls\": [],\n",
      "      \"type\": \"tool_calls\"\n",
      "    },\n",
      "    \"thread_id\": \"thread_rudF1jmbDRotBYIm3RVJIoX0\",\n",
      "    \"type\": \"tool_calls\",\n",
      "    \"usage\": null,\n",
      "    \"expires_at\": 1713409609\n",
      "  },\n",
      "  \"event\": \"thread.run.step.created\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"assistant_id\": \"asst_9UdKwJ8S7iOMrlEX6lzXpLWD\",\n",
      "    \"cancelled_at\": null,\n",
      "    \"completed_at\": null,\n",
      "    \"created_at\": 1713409010,\n",
      "    \"failed_at\": null,\n",
      "    \"last_error\": null,\n",
      "    \"object\": \"thread.run.step\",\n",
      "    \"run_id\": \"run_0PqlNbmtft5Nz6F8eaYsP1vU\",\n",
      "    \"status\": \"in_progress\",\n",
      "    \"step_details\": {\n",
      "      \"tool_calls\": [],\n",
      "      \"type\": \"tool_calls\"\n",
      "    },\n",
      "    \"thread_id\": \"thread_rudF1jmbDRotBYIm3RVJIoX0\",\n",
      "    \"type\": \"tool_calls\",\n",
      "    \"usage\": null,\n",
      "    \"expires_at\": 1713409609\n",
      "  },\n",
      "  \"event\": \"thread.run.step.in_progress\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"id\": \"call_vdwhimGePmlfb1DK1OAUphNJ\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \"\",\n",
      "              \"outputs\": []\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \"from\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \" symp\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \"y\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \" import\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \" symbols\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \",\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \" Eq\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \",\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \" solve\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \"\\n\\n\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \"#\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \" Define\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \" the\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \" variable\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \"\\n\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \"x\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \" =\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \" symbols\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \"('\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \"x\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \"')\\n\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \"#\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \" Define\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \" the\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \" equation\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \"\\n\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \"equ\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \"ation\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \" =\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \" Eq\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \"(\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \"3\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \"*x\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \" +\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \" \"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \"11\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \",\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \" \"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \"14\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \")\\n\\n\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \"#\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \" Solve\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \" the\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \" equation\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \"\\n\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \"solution\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \" =\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \" solve\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \"(e\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \"qu\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \"ation\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \",\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \" x\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \")\\n\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"input\": \"solution\"\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"delta\": {\n",
      "      \"step_details\": {\n",
      "        \"type\": \"tool_calls\",\n",
      "        \"tool_calls\": [\n",
      "          {\n",
      "            \"index\": 0,\n",
      "            \"type\": \"code_interpreter\",\n",
      "            \"code_interpreter\": {\n",
      "              \"outputs\": [\n",
      "                {\n",
      "                  \"index\": 0,\n",
      "                  \"type\": \"logs\",\n",
      "                  \"logs\": \"[1]\"\n",
      "                }\n",
      "              ]\n",
      "            }\n",
      "          }\n",
      "        ]\n",
      "      }\n",
      "    },\n",
      "    \"object\": \"thread.run.step.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.run.step.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_LpKt9GQgocEbEi1SwS19xR25\",\n",
      "    \"assistant_id\": \"asst_9UdKwJ8S7iOMrlEX6lzXpLWD\",\n",
      "    \"cancelled_at\": null,\n",
      "    \"completed_at\": 1713409022,\n",
      "    \"created_at\": 1713409010,\n",
      "    \"failed_at\": null,\n",
      "    \"last_error\": null,\n",
      "    \"object\": \"thread.run.step\",\n",
      "    \"run_id\": \"run_0PqlNbmtft5Nz6F8eaYsP1vU\",\n",
      "    \"status\": \"completed\",\n",
      "    \"step_details\": {\n",
      "      \"tool_calls\": [\n",
      "        {\n",
      "          \"id\": \"call_vdwhimGePmlfb1DK1OAUphNJ\",\n",
      "          \"code_interpreter\": {\n",
      "            \"input\": \"from sympy import symbols, Eq, solve\\n\\n# Define the variable\\nx = symbols('x')\\n# Define the equation\\nequation = Eq(3*x + 11, 14)\\n\\n# Solve the equation\\nsolution = solve(equation, x)\\nsolution\",\n",
      "            \"outputs\": [\n",
      "              {\n",
      "                \"logs\": \"[1]\",\n",
      "                \"type\": \"logs\"\n",
      "              }\n",
      "            ]\n",
      "          },\n",
      "          \"type\": \"code_interpreter\"\n",
      "        }\n",
      "      ],\n",
      "      \"type\": \"tool_calls\"\n",
      "    },\n",
      "    \"thread_id\": \"thread_rudF1jmbDRotBYIm3RVJIoX0\",\n",
      "    \"type\": \"tool_calls\",\n",
      "    \"usage\": {\n",
      "      \"completion_tokens\": 61,\n",
      "      \"prompt_tokens\": 145,\n",
      "      \"total_tokens\": 206\n",
      "    },\n",
      "    \"expires_at\": 1713409609\n",
      "  },\n",
      "  \"event\": \"thread.run.step.completed\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_GyvUWdWillIPmUuJxi2QBym5\",\n",
      "    \"assistant_id\": \"asst_9UdKwJ8S7iOMrlEX6lzXpLWD\",\n",
      "    \"cancelled_at\": null,\n",
      "    \"completed_at\": null,\n",
      "    \"created_at\": 1713409022,\n",
      "    \"failed_at\": null,\n",
      "    \"last_error\": null,\n",
      "    \"object\": \"thread.run.step\",\n",
      "    \"run_id\": \"run_0PqlNbmtft5Nz6F8eaYsP1vU\",\n",
      "    \"status\": \"in_progress\",\n",
      "    \"step_details\": {\n",
      "      \"message_creation\": {\n",
      "        \"message_id\": \"msg_otk4PqGWeK4ciIT5qiWrgC4p\"\n",
      "      },\n",
      "      \"type\": \"message_creation\"\n",
      "    },\n",
      "    \"thread_id\": \"thread_rudF1jmbDRotBYIm3RVJIoX0\",\n",
      "    \"type\": \"message_creation\",\n",
      "    \"usage\": null,\n",
      "    \"expires_at\": 1713409609\n",
      "  },\n",
      "  \"event\": \"thread.run.step.created\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_GyvUWdWillIPmUuJxi2QBym5\",\n",
      "    \"assistant_id\": \"asst_9UdKwJ8S7iOMrlEX6lzXpLWD\",\n",
      "    \"cancelled_at\": null,\n",
      "    \"completed_at\": null,\n",
      "    \"created_at\": 1713409022,\n",
      "    \"failed_at\": null,\n",
      "    \"last_error\": null,\n",
      "    \"object\": \"thread.run.step\",\n",
      "    \"run_id\": \"run_0PqlNbmtft5Nz6F8eaYsP1vU\",\n",
      "    \"status\": \"in_progress\",\n",
      "    \"step_details\": {\n",
      "      \"message_creation\": {\n",
      "        \"message_id\": \"msg_otk4PqGWeK4ciIT5qiWrgC4p\"\n",
      "      },\n",
      "      \"type\": \"message_creation\"\n",
      "    },\n",
      "    \"thread_id\": \"thread_rudF1jmbDRotBYIm3RVJIoX0\",\n",
      "    \"type\": \"message_creation\",\n",
      "    \"usage\": null,\n",
      "    \"expires_at\": 1713409609\n",
      "  },\n",
      "  \"event\": \"thread.run.step.in_progress\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"msg_otk4PqGWeK4ciIT5qiWrgC4p\",\n",
      "    \"assistant_id\": \"asst_9UdKwJ8S7iOMrlEX6lzXpLWD\",\n",
      "    \"completed_at\": null,\n",
      "    \"content\": [],\n",
      "    \"created_at\": 1713409022,\n",
      "    \"file_ids\": [],\n",
      "    \"incomplete_at\": null,\n",
      "    \"incomplete_details\": null,\n",
      "    \"metadata\": {},\n",
      "    \"object\": \"thread.message\",\n",
      "    \"role\": \"assistant\",\n",
      "    \"run_id\": \"run_0PqlNbmtft5Nz6F8eaYsP1vU\",\n",
      "    \"status\": \"in_progress\",\n",
      "    \"thread_id\": \"thread_rudF1jmbDRotBYIm3RVJIoX0\"\n",
      "  },\n",
      "  \"event\": \"thread.message.created\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"msg_otk4PqGWeK4ciIT5qiWrgC4p\",\n",
      "    \"assistant_id\": \"asst_9UdKwJ8S7iOMrlEX6lzXpLWD\",\n",
      "    \"completed_at\": null,\n",
      "    \"content\": [],\n",
      "    \"created_at\": 1713409022,\n",
      "    \"file_ids\": [],\n",
      "    \"incomplete_at\": null,\n",
      "    \"incomplete_details\": null,\n",
      "    \"metadata\": {},\n",
      "    \"object\": \"thread.message\",\n",
      "    \"role\": \"assistant\",\n",
      "    \"run_id\": \"run_0PqlNbmtft5Nz6F8eaYsP1vU\",\n",
      "    \"status\": \"in_progress\",\n",
      "    \"thread_id\": \"thread_rudF1jmbDRotBYIm3RVJIoX0\"\n",
      "  },\n",
      "  \"event\": \"thread.message.in_progress\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"msg_otk4PqGWeK4ciIT5qiWrgC4p\",\n",
      "    \"delta\": {\n",
      "      \"content\": [\n",
      "        {\n",
      "          \"index\": 0,\n",
      "          \"type\": \"text\",\n",
      "          \"text\": {\n",
      "            \"annotations\": [],\n",
      "            \"value\": \"The\"\n",
      "          }\n",
      "        }\n",
      "      ]\n",
      "    },\n",
      "    \"object\": \"thread.message.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.message.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"msg_otk4PqGWeK4ciIT5qiWrgC4p\",\n",
      "    \"delta\": {\n",
      "      \"content\": [\n",
      "        {\n",
      "          \"index\": 0,\n",
      "          \"type\": \"text\",\n",
      "          \"text\": {\n",
      "            \"value\": \" solution\"\n",
      "          }\n",
      "        }\n",
      "      ]\n",
      "    },\n",
      "    \"object\": \"thread.message.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.message.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"msg_otk4PqGWeK4ciIT5qiWrgC4p\",\n",
      "    \"delta\": {\n",
      "      \"content\": [\n",
      "        {\n",
      "          \"index\": 0,\n",
      "          \"type\": \"text\",\n",
      "          \"text\": {\n",
      "            \"value\": \" to\"\n",
      "          }\n",
      "        }\n",
      "      ]\n",
      "    },\n",
      "    \"object\": \"thread.message.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.message.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"msg_otk4PqGWeK4ciIT5qiWrgC4p\",\n",
      "    \"delta\": {\n",
      "      \"content\": [\n",
      "        {\n",
      "          \"index\": 0,\n",
      "          \"type\": \"text\",\n",
      "          \"text\": {\n",
      "            \"value\": \" the\"\n",
      "          }\n",
      "        }\n",
      "      ]\n",
      "    },\n",
      "    \"object\": \"thread.message.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.message.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"msg_otk4PqGWeK4ciIT5qiWrgC4p\",\n",
      "    \"delta\": {\n",
      "      \"content\": [\n",
      "        {\n",
      "          \"index\": 0,\n",
      "          \"type\": \"text\",\n",
      "          \"text\": {\n",
      "            \"value\": \" equation\"\n",
      "          }\n",
      "        }\n",
      "      ]\n",
      "    },\n",
      "    \"object\": \"thread.message.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.message.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"msg_otk4PqGWeK4ciIT5qiWrgC4p\",\n",
      "    \"delta\": {\n",
      "      \"content\": [\n",
      "        {\n",
      "          \"index\": 0,\n",
      "          \"type\": \"text\",\n",
      "          \"text\": {\n",
      "            \"value\": \" \\\\(\"\n",
      "          }\n",
      "        }\n",
      "      ]\n",
      "    },\n",
      "    \"object\": \"thread.message.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.message.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"msg_otk4PqGWeK4ciIT5qiWrgC4p\",\n",
      "    \"delta\": {\n",
      "      \"content\": [\n",
      "        {\n",
      "          \"index\": 0,\n",
      "          \"type\": \"text\",\n",
      "          \"text\": {\n",
      "            \"value\": \"3\"\n",
      "          }\n",
      "        }\n",
      "      ]\n",
      "    },\n",
      "    \"object\": \"thread.message.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.message.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"msg_otk4PqGWeK4ciIT5qiWrgC4p\",\n",
      "    \"delta\": {\n",
      "      \"content\": [\n",
      "        {\n",
      "          \"index\": 0,\n",
      "          \"type\": \"text\",\n",
      "          \"text\": {\n",
      "            \"value\": \"x\"\n",
      "          }\n",
      "        }\n",
      "      ]\n",
      "    },\n",
      "    \"object\": \"thread.message.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.message.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"msg_otk4PqGWeK4ciIT5qiWrgC4p\",\n",
      "    \"delta\": {\n",
      "      \"content\": [\n",
      "        {\n",
      "          \"index\": 0,\n",
      "          \"type\": \"text\",\n",
      "          \"text\": {\n",
      "            \"value\": \" +\"\n",
      "          }\n",
      "        }\n",
      "      ]\n",
      "    },\n",
      "    \"object\": \"thread.message.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.message.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"msg_otk4PqGWeK4ciIT5qiWrgC4p\",\n",
      "    \"delta\": {\n",
      "      \"content\": [\n",
      "        {\n",
      "          \"index\": 0,\n",
      "          \"type\": \"text\",\n",
      "          \"text\": {\n",
      "            \"value\": \" \"\n",
      "          }\n",
      "        }\n",
      "      ]\n",
      "    },\n",
      "    \"object\": \"thread.message.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.message.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"msg_otk4PqGWeK4ciIT5qiWrgC4p\",\n",
      "    \"delta\": {\n",
      "      \"content\": [\n",
      "        {\n",
      "          \"index\": 0,\n",
      "          \"type\": \"text\",\n",
      "          \"text\": {\n",
      "            \"value\": \"11\"\n",
      "          }\n",
      "        }\n",
      "      ]\n",
      "    },\n",
      "    \"object\": \"thread.message.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.message.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"msg_otk4PqGWeK4ciIT5qiWrgC4p\",\n",
      "    \"delta\": {\n",
      "      \"content\": [\n",
      "        {\n",
      "          \"index\": 0,\n",
      "          \"type\": \"text\",\n",
      "          \"text\": {\n",
      "            \"value\": \" =\"\n",
      "          }\n",
      "        }\n",
      "      ]\n",
      "    },\n",
      "    \"object\": \"thread.message.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.message.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"msg_otk4PqGWeK4ciIT5qiWrgC4p\",\n",
      "    \"delta\": {\n",
      "      \"content\": [\n",
      "        {\n",
      "          \"index\": 0,\n",
      "          \"type\": \"text\",\n",
      "          \"text\": {\n",
      "            \"value\": \" \"\n",
      "          }\n",
      "        }\n",
      "      ]\n",
      "    },\n",
      "    \"object\": \"thread.message.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.message.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"msg_otk4PqGWeK4ciIT5qiWrgC4p\",\n",
      "    \"delta\": {\n",
      "      \"content\": [\n",
      "        {\n",
      "          \"index\": 0,\n",
      "          \"type\": \"text\",\n",
      "          \"text\": {\n",
      "            \"value\": \"14\"\n",
      "          }\n",
      "        }\n",
      "      ]\n",
      "    },\n",
      "    \"object\": \"thread.message.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.message.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"msg_otk4PqGWeK4ciIT5qiWrgC4p\",\n",
      "    \"delta\": {\n",
      "      \"content\": [\n",
      "        {\n",
      "          \"index\": 0,\n",
      "          \"type\": \"text\",\n",
      "          \"text\": {\n",
      "            \"value\": \"\\\\)\"\n",
      "          }\n",
      "        }\n",
      "      ]\n",
      "    },\n",
      "    \"object\": \"thread.message.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.message.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"msg_otk4PqGWeK4ciIT5qiWrgC4p\",\n",
      "    \"delta\": {\n",
      "      \"content\": [\n",
      "        {\n",
      "          \"index\": 0,\n",
      "          \"type\": \"text\",\n",
      "          \"text\": {\n",
      "            \"value\": \" is\"\n",
      "          }\n",
      "        }\n",
      "      ]\n",
      "    },\n",
      "    \"object\": \"thread.message.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.message.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"msg_otk4PqGWeK4ciIT5qiWrgC4p\",\n",
      "    \"delta\": {\n",
      "      \"content\": [\n",
      "        {\n",
      "          \"index\": 0,\n",
      "          \"type\": \"text\",\n",
      "          \"text\": {\n",
      "            \"value\": \" \\\\(\"\n",
      "          }\n",
      "        }\n",
      "      ]\n",
      "    },\n",
      "    \"object\": \"thread.message.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.message.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"msg_otk4PqGWeK4ciIT5qiWrgC4p\",\n",
      "    \"delta\": {\n",
      "      \"content\": [\n",
      "        {\n",
      "          \"index\": 0,\n",
      "          \"type\": \"text\",\n",
      "          \"text\": {\n",
      "            \"value\": \"x\"\n",
      "          }\n",
      "        }\n",
      "      ]\n",
      "    },\n",
      "    \"object\": \"thread.message.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.message.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"msg_otk4PqGWeK4ciIT5qiWrgC4p\",\n",
      "    \"delta\": {\n",
      "      \"content\": [\n",
      "        {\n",
      "          \"index\": 0,\n",
      "          \"type\": \"text\",\n",
      "          \"text\": {\n",
      "            \"value\": \" =\"\n",
      "          }\n",
      "        }\n",
      "      ]\n",
      "    },\n",
      "    \"object\": \"thread.message.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.message.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"msg_otk4PqGWeK4ciIT5qiWrgC4p\",\n",
      "    \"delta\": {\n",
      "      \"content\": [\n",
      "        {\n",
      "          \"index\": 0,\n",
      "          \"type\": \"text\",\n",
      "          \"text\": {\n",
      "            \"value\": \" \"\n",
      "          }\n",
      "        }\n",
      "      ]\n",
      "    },\n",
      "    \"object\": \"thread.message.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.message.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"msg_otk4PqGWeK4ciIT5qiWrgC4p\",\n",
      "    \"delta\": {\n",
      "      \"content\": [\n",
      "        {\n",
      "          \"index\": 0,\n",
      "          \"type\": \"text\",\n",
      "          \"text\": {\n",
      "            \"value\": \"1\"\n",
      "          }\n",
      "        }\n",
      "      ]\n",
      "    },\n",
      "    \"object\": \"thread.message.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.message.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"msg_otk4PqGWeK4ciIT5qiWrgC4p\",\n",
      "    \"delta\": {\n",
      "      \"content\": [\n",
      "        {\n",
      "          \"index\": 0,\n",
      "          \"type\": \"text\",\n",
      "          \"text\": {\n",
      "            \"value\": \"\\\\\"\n",
      "          }\n",
      "        }\n",
      "      ]\n",
      "    },\n",
      "    \"object\": \"thread.message.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.message.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"msg_otk4PqGWeK4ciIT5qiWrgC4p\",\n",
      "    \"delta\": {\n",
      "      \"content\": [\n",
      "        {\n",
      "          \"index\": 0,\n",
      "          \"type\": \"text\",\n",
      "          \"text\": {\n",
      "            \"value\": \").\"\n",
      "          }\n",
      "        }\n",
      "      ]\n",
      "    },\n",
      "    \"object\": \"thread.message.delta\"\n",
      "  },\n",
      "  \"event\": \"thread.message.delta\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"msg_otk4PqGWeK4ciIT5qiWrgC4p\",\n",
      "    \"assistant_id\": \"asst_9UdKwJ8S7iOMrlEX6lzXpLWD\",\n",
      "    \"completed_at\": 1713409023,\n",
      "    \"content\": [\n",
      "      {\n",
      "        \"text\": {\n",
      "          \"annotations\": [],\n",
      "          \"value\": \"The solution to the equation \\\\(3x + 11 = 14\\\\) is \\\\(x = 1\\\\).\"\n",
      "        },\n",
      "        \"type\": \"text\"\n",
      "      }\n",
      "    ],\n",
      "    \"created_at\": 1713409022,\n",
      "    \"file_ids\": [],\n",
      "    \"incomplete_at\": null,\n",
      "    \"incomplete_details\": null,\n",
      "    \"metadata\": {},\n",
      "    \"object\": \"thread.message\",\n",
      "    \"role\": \"assistant\",\n",
      "    \"run_id\": \"run_0PqlNbmtft5Nz6F8eaYsP1vU\",\n",
      "    \"status\": \"completed\",\n",
      "    \"thread_id\": \"thread_rudF1jmbDRotBYIm3RVJIoX0\"\n",
      "  },\n",
      "  \"event\": \"thread.message.completed\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"step_GyvUWdWillIPmUuJxi2QBym5\",\n",
      "    \"assistant_id\": \"asst_9UdKwJ8S7iOMrlEX6lzXpLWD\",\n",
      "    \"cancelled_at\": null,\n",
      "    \"completed_at\": 1713409023,\n",
      "    \"created_at\": 1713409022,\n",
      "    \"failed_at\": null,\n",
      "    \"last_error\": null,\n",
      "    \"object\": \"thread.run.step\",\n",
      "    \"run_id\": \"run_0PqlNbmtft5Nz6F8eaYsP1vU\",\n",
      "    \"status\": \"completed\",\n",
      "    \"step_details\": {\n",
      "      \"message_creation\": {\n",
      "        \"message_id\": \"msg_otk4PqGWeK4ciIT5qiWrgC4p\"\n",
      "      },\n",
      "      \"type\": \"message_creation\"\n",
      "    },\n",
      "    \"thread_id\": \"thread_rudF1jmbDRotBYIm3RVJIoX0\",\n",
      "    \"type\": \"message_creation\",\n",
      "    \"usage\": {\n",
      "      \"completion_tokens\": 25,\n",
      "      \"prompt_tokens\": 217,\n",
      "      \"total_tokens\": 242\n",
      "    },\n",
      "    \"expires_at\": 1713409609\n",
      "  },\n",
      "  \"event\": \"thread.run.step.completed\"\n",
      "}\n",
      "{\n",
      "  \"data\": {\n",
      "    \"id\": \"run_0PqlNbmtft5Nz6F8eaYsP1vU\",\n",
      "    \"assistant_id\": \"asst_9UdKwJ8S7iOMrlEX6lzXpLWD\",\n",
      "    \"cancelled_at\": null,\n",
      "    \"completed_at\": 1713409023,\n",
      "    \"created_at\": 1713409009,\n",
      "    \"expires_at\": null,\n",
      "    \"failed_at\": null,\n",
      "    \"file_ids\": [],\n",
      "    \"incomplete_details\": null,\n",
      "    \"instructions\": \"Please address the user as Jane Doe. The user has a premium account.\",\n",
      "    \"last_error\": null,\n",
      "    \"max_completion_tokens\": null,\n",
      "    \"max_prompt_tokens\": null,\n",
      "    \"metadata\": {},\n",
      "    \"model\": \"gpt-4-1106-preview\",\n",
      "    \"object\": \"thread.run\",\n",
      "    \"required_action\": null,\n",
      "    \"response_format\": \"auto\",\n",
      "    \"started_at\": 1713409009,\n",
      "    \"status\": \"completed\",\n",
      "    \"thread_id\": \"thread_rudF1jmbDRotBYIm3RVJIoX0\",\n",
      "    \"tool_choice\": \"auto\",\n",
      "    \"tools\": [\n",
      "      {\n",
      "        \"type\": \"code_interpreter\"\n",
      "      }\n",
      "    ],\n",
      "    \"truncation_strategy\": {\n",
      "      \"type\": \"auto\",\n",
      "      \"last_messages\": null\n",
      "    },\n",
      "    \"usage\": {\n",
      "      \"completion_tokens\": 86,\n",
      "      \"prompt_tokens\": 362,\n",
      "      \"total_tokens\": 448\n",
      "    },\n",
      "    \"temperature\": 1.0,\n",
      "    \"top_p\": 1.0\n",
      "  },\n",
      "  \"event\": \"thread.run.completed\"\n",
      "}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "AssistantDeleted(id='asst_9UdKwJ8S7iOMrlEX6lzXpLWD', deleted=True, object='assistant.deleted')"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import openai\n",
    "\n",
    "# 从环境变量 OPENAI_API_KEY 中获取 API 密钥\n",
    "client = openai.OpenAI()\n",
    "\n",
    "# 创建一个名为 \"Math Tutor\" 的助手，它是一个个人数学辅导老师。这个助手能够编写并运行代码来解答数学问题。\n",
    "assistant = client.beta.assistants.create(\n",
    "    name=\"Math Tutor\",\n",
    "    instructions=\"You are a personal math tutor. Write and run code to answer math questions.\",\n",
    "    tools=[{\"type\": \"code_interpreter\"}],  # 工具包括代码解释器\n",
    "    model=\"gpt-4-1106-preview\",  # 使用的模型是 GPT-4\n",
    ")\n",
    "\n",
    "# 创建一个交流线程\n",
    "thread = client.beta.threads.create()\n",
    "\n",
    "# 在该线程中创建一条消息，表示用户角色，并提交一个数学问题：“我需要解方程 `3x + 11 = 14`。你能帮忙吗？”\n",
    "message = client.beta.threads.messages.create(\n",
    "    thread_id=thread.id,\n",
    "    role=\"user\",\n",
    "    content=\"I need to solve the equation `3x + 11 = 14`. Can you help me?\",\n",
    ")\n",
    "\n",
    "print(\"starting run stream\")  # 打印开始执行流的消息\n",
    "\n",
    "# 创建一个执行流，用于处理该线程中的交互和问题解答\n",
    "stream = client.beta.threads.runs.create(\n",
    "    thread_id=thread.id,\n",
    "    assistant_id=assistant.id,\n",
    "    instructions=\"Please address the user as Jane Doe. The user has a premium account.\",  # 以 Jane Doe 称呼用户，并且用户拥有高级账户\n",
    "    stream=True,  # 持续流式传输\n",
    ")\n",
    "\n",
    "# 遍历执行流中的事件，并以 JSON 格式打印它们\n",
    "for event in stream:\n",
    "    print(event.model_dump_json(indent=2, exclude_unset=True))\n",
    "\n",
    "# 删除创建的助手\n",
    "client.beta.assistants.delete(assistant.id)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b68ff9fa-e815-444b-b9e3-9dc062a0cc00",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ff800570-5539-43a2-8c36-f6e7958c112a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "070e5f61-1931-46b6-a025-72da8116a125",
   "metadata": {},
   "source": [
    "## 开发 Python 代码小助手"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e0604c6b-5227-454a-a036-3cbe8f071d7e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai  # 导入 openai 库\n",
    "\n",
    "# 从环境变量 OPENAI_API_KEY 中获取 API 密钥\n",
    "client = openai.OpenAI()\n",
    "\n",
    "# 创建一个名为 \"Python Master\" 的助手，它能根据需求生成可以运行的 Python 代码\n",
    "assistant_python = client.beta.assistants.create(\n",
    "    name=\"Python Master\",\n",
    "    instructions=\"You are a Python Expert. Generate runnable Python code according to messages.\",\n",
    "    tools=[{\"type\": \"code_interpreter\"}],  # 使用工具：代码解释器\n",
    "    model=\"gpt-4-1106-preview\",  # 使用模型： GPT-4\n",
    ")\n",
    "\n",
    "# 创建一个交流线程\n",
    "thread_python = client.beta.threads.create()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ca4de29a-c87f-4f4a-9034-9aed4a82f641",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'asst_ZUGg9t7ewn9iRI966OiNSwts'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "assistant_python.id"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "610371e1-82bf-47e5-bbb4-63f3bec8e7f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 在该线程中创建一条信息\n",
    "message = client.beta.threads.messages.create(\n",
    "    thread_id=thread_python.id,\n",
    "    role=\"user\",\n",
    "    content=\"快速排序咋个写？\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "ae385730-7d50-45ca-af53-748cf641a1a7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Run completed with status: completed\n"
     ]
    }
   ],
   "source": [
    "# 创建并等待执行流完成，用于处理该线程中的交互和问题解答\n",
    "run = client.beta.threads.runs.create_and_poll(\n",
    "    thread_id=thread_python.id,\n",
    "    assistant_id=assistant_python.id,\n",
    ")\n",
    "\n",
    "print(\"Run completed with status: \" + run.status)  # 打印执行流的完成状态"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "83469189-458d-4130-9e92-a814043ff813",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Messages:\n",
      "\n",
      "Role: Assistant\n",
      "Message:\n",
      "快速排序算法（Quick Sort）是一种常见的排序算法，它采用了分治策略。在平均状况下，排序n个项目要O(n log n)次比较。在最坏状况下则需要O(n^2)次比较，但这种状况并不常见。实际上，快速排序通常明显比其他Ο(n log n)算法更快，因为它的内部循环（inner loop）可以很有效地在大部分的架构上运行。\n",
      "\n",
      "基本思想是：\n",
      "1. 从数列中挑出一个元素，称为\"基准\"（pivot），\n",
      "2. 重新排序数列，所有比基准值小的元素摆放在基准前面，所有比基准值大的元素摆在基准的后面（相同的数可以到任一边）。在这个分区退出之后，该基准就处于数列的中间位置。这个称为分区（partition）操作。\n",
      "3. 递归地（recursive）把小于基准值元素的子数列和大于基准值元素的子数列排序。\n",
      "\n",
      "下面是一个快速排序的Python实现示例：\n",
      "\n",
      "```python\n",
      "def quick_sort(arr):\n",
      "    # 辅助递归函数，使用数组切片\n",
      "    def _quick_sort(items, low, high):\n",
      "        if low < high:\n",
      "            pivot_index = partition(items, low, high)\n",
      "            _quick_sort(items, low, pivot_index)  # 递归排序基准左侧\n",
      "            _quick_sort(items, pivot_index + 1, high)  # 递归排序基准右侧\n",
      "            \n",
      "    def partition(array, low, high):\n",
      "        # 选择中间的值为基准，以防止对于近乎排序好的数组性能差\n",
      "        pivot = array[(low + high) // 2]\n",
      "        left = low\n",
      "        right = high\n",
      "        while True:\n",
      "            while array[left] < pivot: # 从左向右找到第一个大于等于pivot的值\n",
      "                left += 1\n",
      "            while array[right] > pivot: # 从右向左找到第一个小于等于pivot的值\n",
      "                right -= 1\n",
      "            if left >= right: # 如果两个指针相遇，分区完成\n",
      "                return right\n",
      "            # 如果左边的值大于基准，而右边的值小于基准，交换它们\n",
      "            array[left], array[right] = array[right], array[left]\n",
      "            left += 1\n",
      "            right -= 1\n",
      "\n",
      "    _quick_sort(arr, 0, len(arr) - 1)\n",
      "    return arr\n",
      "```\n",
      "\n",
      "这段代码实现了经典的快速排序算法，并进行了适度的优化。这个快速排序版本使用了基准值选择的优化，并且通过递归调用实现了分区操作的递归。如果你想我测试这段代码，请提供一个数组。\n",
      "\n",
      "Role: User\n",
      "Message:\n",
      "快速排序咋个写？\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 如果执行流状态为 \"completed\"（已完成），则获取并打印所有消息\n",
    "if run.status == \"completed\":\n",
    "    messages = client.beta.threads.messages.list(thread_id=thread_python.id)\n",
    "\n",
    "    print(\"\\nMessages:\\n\")\n",
    "    for message in messages:\n",
    "        assert message.content[0].type == \"text\"\n",
    "        print(f\"Role: {message.role.capitalize()}\")  # 角色名称首字母大写\n",
    "        print(\"Message:\")\n",
    "        print(message.content[0].text.value + \"\\n\")  # 每条消息后添加空行以增加可读性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "f2b351d8-8af7-401d-936b-377acfad2b2e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 在该线程中创建一条信息\n",
    "message = client.beta.threads.messages.create(\n",
    "    thread_id=thread_python.id,\n",
    "    role=\"user\",\n",
    "    content=\"红黑树呢？\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a664dc7f-399c-4793-95a0-1bca7b37847c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Run completed with status: completed\n"
     ]
    }
   ],
   "source": [
    "# 创建并等待执行流完成，用于处理该线程中的交互和问题解答\n",
    "run = client.beta.threads.runs.create_and_poll(\n",
    "    thread_id=thread_python.id,\n",
    "    assistant_id=assistant_python.id,\n",
    ")\n",
    "\n",
    "print(\"Run completed with status: \" + run.status)  # 打印执行流的完成状态"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "4a76261f-aae9-499e-91d8-0ffc55de8bd3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Messages:\n",
      "\n",
      "Role: Assistant\n",
      "Message:\n",
      "红黑树是一种自平衡二叉查找树，它在插入和删除等动态集合操作时，可以保证二叉树的平衡，因此查找操作始终具有对数时间复杂度。每个节点都遵循红黑树的性质:\n",
      "\n",
      "1. 每个节点要么是红的，要么是黑的。\n",
      "2. 根节点是黑的。\n",
      "3. 所有叶子（NIL节点，空节点）都是黑的。\n",
      "4. 每个红色节点必须有两个黑色的子节点。（从每个叶子到根的所有路径上不能有两个连续的红色节点）\n",
      "5. 从任一节点到其每个叶子的所有简单路径都包含相同数目的黑色节点。\n",
      "\n",
      "由于红黑树实现相对复杂，通常标准库都会提供实现，比如C++的`std::map`以红黑树为基础实现，Java中的`TreeMap`也是。在Python中，并没有直接的红黑树实现，但是内建的排序字典(`collections.OrderedDict`)和集合(`set`)都实现了类似的平衡树结构。\n",
      "\n",
      "以下是一个非常基本的红黑树节点定义的示例，不包含完整的红黑树操作实现。完整的红黑树操作（如插入、删除、平衡操作等）需要更详细的代码实现和大量的边界条件处理。\n",
      "\n",
      "```python\n",
      "class Node:\n",
      "    def __init__(self, color, key, left=None, right=None, p=None):\n",
      "        self.color = color  # 节点颜色（红或黑）\n",
      "        self.key = key      # 节点关键字\n",
      "        self.left = left    # 左子节点\n",
      "        self.right = right  # 右子节点\n",
      "        self.p = p          # 父节点\n",
      "\n",
      "class RedBlackTree:\n",
      "    def __init__(self):\n",
      "        self.NIL = Node('BLACK', None)  # 初始化NIL节点\n",
      "        self.root = self.NIL            # 树的根指向NIL节点\n",
      "\n",
      "    # 其他操作，如插入、删除等...\n",
      "```\n",
      "\n",
      "实际插入和删除节点时，需要对树进行旋转和重新着色，以保证上述红黑树的性质。这些操作比较复杂，涉及许多特殊情况的处理。由于篇幅原因，我没有在这里提供完整的实现。如果你需要红黑树的一个库，你可以在Python中使用第三方库，如`rbtree`。\n",
      "\n",
      "请注意，如果你需要红黑树的具体实现，并想要在Python中使用它，我建议寻找一个可靠的第三方库，这可以节省很多时间，并减少错误。如果你只是希望学习算法本身的话，那么我建议阅读更详细的数据结构与算法书籍中的红黑树章节或者查找在线教程和资料，以获得完整的理解和实现方法。\n",
      "\n",
      "Role: User\n",
      "Message:\n",
      "红黑树呢？\n",
      "\n",
      "Role: Assistant\n",
      "Message:\n",
      "快速排序算法（Quick Sort）是一种常见的排序算法，它采用了分治策略。在平均状况下，排序n个项目要O(n log n)次比较。在最坏状况下则需要O(n^2)次比较，但这种状况并不常见。实际上，快速排序通常明显比其他Ο(n log n)算法更快，因为它的内部循环（inner loop）可以很有效地在大部分的架构上运行。\n",
      "\n",
      "基本思想是：\n",
      "1. 从数列中挑出一个元素，称为\"基准\"（pivot），\n",
      "2. 重新排序数列，所有比基准值小的元素摆放在基准前面，所有比基准值大的元素摆在基准的后面（相同的数可以到任一边）。在这个分区退出之后，该基准就处于数列的中间位置。这个称为分区（partition）操作。\n",
      "3. 递归地（recursive）把小于基准值元素的子数列和大于基准值元素的子数列排序。\n",
      "\n",
      "下面是一个快速排序的Python实现示例：\n",
      "\n",
      "```python\n",
      "def quick_sort(arr):\n",
      "    # 辅助递归函数，使用数组切片\n",
      "    def _quick_sort(items, low, high):\n",
      "        if low < high:\n",
      "            pivot_index = partition(items, low, high)\n",
      "            _quick_sort(items, low, pivot_index)  # 递归排序基准左侧\n",
      "            _quick_sort(items, pivot_index + 1, high)  # 递归排序基准右侧\n",
      "            \n",
      "    def partition(array, low, high):\n",
      "        # 选择中间的值为基准，以防止对于近乎排序好的数组性能差\n",
      "        pivot = array[(low + high) // 2]\n",
      "        left = low\n",
      "        right = high\n",
      "        while True:\n",
      "            while array[left] < pivot: # 从左向右找到第一个大于等于pivot的值\n",
      "                left += 1\n",
      "            while array[right] > pivot: # 从右向左找到第一个小于等于pivot的值\n",
      "                right -= 1\n",
      "            if left >= right: # 如果两个指针相遇，分区完成\n",
      "                return right\n",
      "            # 如果左边的值大于基准，而右边的值小于基准，交换它们\n",
      "            array[left], array[right] = array[right], array[left]\n",
      "            left += 1\n",
      "            right -= 1\n",
      "\n",
      "    _quick_sort(arr, 0, len(arr) - 1)\n",
      "    return arr\n",
      "```\n",
      "\n",
      "这段代码实现了经典的快速排序算法，并进行了适度的优化。这个快速排序版本使用了基准值选择的优化，并且通过递归调用实现了分区操作的递归。如果你想我测试这段代码，请提供一个数组。\n",
      "\n",
      "Role: User\n",
      "Message:\n",
      "快速排序咋个写？\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 如果执行流状态为 \"completed\"（已完成），则获取并打印所有消息\n",
    "if run.status == \"completed\":\n",
    "    messages = client.beta.threads.messages.list(thread_id=thread_python.id)\n",
    "\n",
    "    print(\"\\nMessages:\\n\")\n",
    "    for message in messages:\n",
    "        assert message.content[0].type == \"text\"\n",
    "        print(f\"Role: {message.role.capitalize()}\")  # 角色名称首字母大写\n",
    "        print(\"Message:\")\n",
    "        print(message.content[0].text.value + \"\\n\")  # 每条消息后添加空行以增加可读性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "76f126e2-64a2-4887-810c-b5059d5bcf56",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "788179ec-d10e-4cbd-b18b-1cf2b5fa02ee",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "e49e1c65-626f-473c-80c9-2be33bc0a98f",
   "metadata": {},
   "source": [
    "## Homework: 将数学辅导老师和Python小助手的代码重构，实现类似 Playground 的对话输入体验"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "541ad829-e6c7-4316-ab2f-03005610fc86",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
